package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);

        dishService.save(dishDto);
        return R.success("添加成功");
    }

    //多表连接分页查询
    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page,Integer pageSize ,String name ){
        Page<DishDto> pageObj = dishService.findPage2(page, pageSize, name);
        return R.success(pageObj);
    }

    //指定固定id查询菜品
    @GetMapping("/{id}")
    public R<DishDto>findById(@PathVariable Long id){
        //调用有任务层
        DishDto dishDao = dishService.findById(id);
        return R.success(dishDao);

    }

  /*//显示指定类别的菜品列表数据
    @RequestMapping("/list")
    public R<List<Dish>>findByCategoryId(Long categoryId){
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        return R.success(dishList);
    }
*/
 //根据类别和状态查询菜品的列表
    @RequestMapping("/list")
    public R<List<DishDto>>findByCategoryId(Long categoryId,Integer status){
        List<DishDto> dishLish = dishService.findByCategoryId(categoryId, status);

        return R.success(dishLish);

    }

}
